package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.Instance.character.CharacterPunishInstance;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1GroundInventory;
import com.lineage.server.model.L1Inventory;
import com.lineage.server.serverpackets.S_ChangeHeading;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_PacketBoxGree;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_PickUpItem extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_PickUpItem.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
        return; 
      pc.isGhost();
      if (pc.isDead() || pc.isTeleport() || pc.isPrivateShop() || pc.isInvisble() || pc.isInvisDelay())
        return; 
      int x = readH();
      int y = readH();
      int objectId = readD();
      long pickupCount = readD();
      if (pickupCount > 2147483647L)
        pickupCount = 2147483647L; 
      pickupCount = Math.max(0L, pickupCount);
      L1GroundInventory l1GroundInventory = World.get().getInventory(x, y, pc.getMapId());
      L1ItemInstance l1ItemInstance = l1GroundInventory.getItem(objectId);
      if (l1ItemInstance != null && !pc.isDead()) {
        L1ItemInstance item = l1ItemInstance;
        if (item.getCount() <= 0L)
          return; 
        if (item.getItemOwnerId() != 0 && pc.getId() != item.getItemOwnerId()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(623));
          return;
        } 
        if (pc.getLocation().getTileLineDistance((Point)item.getLocation()) > 3)
          return; 
        item.set_showId(-1);
        if (pc.getInventory().checkAddItem(item, pickupCount) == 0 && item.getX() != 0 && item.getY() != 0) {
          if (ConfigOther.Pickupitemtoall && item.getItem().getItemId() != 40515)
            World.get().broadcastPacketToAll((ServerBasePacket)new S_ServerMessage(
                  "玩家[" + pc.getName() + "]捡起地上物品[" + item.getItem().getName() + "]")); 
          _log.info(
              "人物:" + pc.getName() + " 捡起物品 " + item.getItem().getName() + " 物品OBJID:" + item.getId());
          l1GroundInventory.tradeItem(item, pickupCount, (L1Inventory)pc.getInventory());
          pc.turnOnOffLight();
          pc.setHeading(pc.targetDirection(item.getX(), item.getY()));
          if (!pc.isGmInvis()) {
            pc.broadcastPacketAll((ServerBasePacket)new S_ChangeHeading((L1Character)pc));
            pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 15));
          } 
          if (World.checkpcFallingItem(objectId)) {
            L1ItemInstance fallingItem = World.getPcFallingItem(objectId);
            String charName = CharacterTable.get().getCharName(fallingItem.get_char_objid());
            String message = String.format("玩家【%s】，捡起了玩家【%s】的【%s】", new Object[] { pc.getName(), charName, item.getLogName() });
            World.get().broadcastPacketToAll((ServerBasePacket)new S_PacketBoxGree(2, "\\f=" + message));
            World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("\\f=" + message));
            World.removepcFallingItemObjId(objectId);
          } 
          RecordTable.get().recordtakeitem(pc.getName(), item.getAllName(), (int)pickupCount, item.getId(), 
              pc.getIp());
        } 
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
